package DAO;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import anroid.vstore.provider.Helper;
import DTO.NotificationDTO;
import DTO.ProductDTO;

public class NotificationDAO {
	public static Integer NewNotification()
	{
		String url = Helper.SERVICE_URL + "api/newstoday.php";
		String jsonStr = ServiceHandler.makeServiceCall(url, ServiceHandler.GET);
		try {
			JSONArray jsonArr = new JSONArray(jsonStr);
			return Integer.parseInt(jsonArr.getJSONObject(0).getString("number"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	public static ArrayList<NotificationDTO> GetNotifications(int from, int to)
	{
		ArrayList<NotificationDTO> list = new ArrayList<NotificationDTO>();
		String url = Helper.SERVICE_URL + "api/event.php?";
		url += "from=" + from;
		url += "&to=" + to;
		
		String jsonStr = ServiceHandler.makeServiceCall(url, ServiceHandler.GET);
		if (jsonStr != null) {
			try {
				JSONArray jsonArr = new JSONArray(jsonStr);
				for(int i =0 ; i < jsonArr.length();i++)
				{
					JSONObject c = jsonArr.getJSONObject(i);
					NotificationDTO notf = new NotificationDTO();
					notf.id = c.getInt("Id");
					notf.name = c.getString("name");
					notf.subtitle = c.getString("subTitle");
					notf.msg = c.getString("content");
					notf.imgURL = c.getString("thumbImg");
					notf.refId = c.getInt("refId");
					
					
					list.add(notf);
				}
				
			}catch(Exception e){
				}
			}
		return list;
	}
}
